package cn.llyong.rpc.client.config;

import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotationMetadata;

import cn.llyong.rpc.client.annotation.RpcServiceScan;

/**
 * @description: 包扫描注册为bean
 * @author: lvyong
 * @date: 2020年06月30日 23:47
 */
public class RpcServiceScannerRegistrar implements ImportBeanDefinitionRegistrar, ResourceLoaderAware {

    private ResourceLoader resourceLoader;

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        AnnotationAttributes attributes = AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes(RpcServiceScan.class.getName()));
        String[] basePackages = attributes.getStringArray("basePackages");
        if (basePackages == null || basePackages.length == 0) {
            return;
        }
        RpcServiceDefinitionScanner scanner = new RpcServiceDefinitionScanner(registry);
        scanner.doScan(basePackages);
    }

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }
}
